package com.dlm.controller;

import com.dlm.dao.Evaluate;
import com.dlm.dao.UserCoupon;
import com.dlm.sdk.api.OpenIdApi;
import com.dlm.service.IEvaluateService;
import com.dlm.service.IUserCouponService;
import com.dlm.service.impl.EvaluateServiceImpl;
import com.dlm.service.impl.UserCouponServiceImpl;
import com.google.common.base.Preconditions;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Created by chongsong on 15/2/1.
 * <mailto>chong.song@autonavi.com</mailto>
 */
public class UserCouponController extends BaseController {
    private Logger logger = Logger.getLogger(UserCouponController.class);
    private IUserCouponService service = new UserCouponServiceImpl();

    public void add() {
        renderJson(makeDto(1, "保存失败!", null));
    }

    public void del() {
        long id = getParaToLong("id", 0l);
        try {
            Preconditions.checkArgument(id > 0, "id不能为空且必须大于0!");
        } catch (Exception e) {
            renderJson(makeDto(1, e.getMessage(), null));
            return;
        }

        boolean isDel = false;
        try {
            isDel = service.delete(id);
            if (isDel) {
                renderJson(makeDto(0, "ok", null));
                return;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        renderJson(makeDto(1, "删除失败", null));
    }

    public void find() {
        int pno = getParaToInt("pno", 1);
        int psize = getParaToInt("psize", 10);
        int isUseful = getParaToInt("isUseful",0);
        long phone = -1;
        try {
            if(null == getSession().getAttribute("phone")){
                renderJson(makeDto(100, "查询失败", null));
                return;
            }
            phone = (Long) getSession().getAttribute("phone");
        } catch (Exception e) {
            renderJson(makeDto(1, e.getMessage(), null));
            return;
        }
        try {
            if(phone > 0){
                Page<UserCoupon> page = service.findByPage(pno, psize,phone,isUseful);
                renderJson(makeDtoPage(0, "ok", page));
                return;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        renderJson(makeDto(1, "查询失败", null));
    }

}
